<?php
// 本类由系统自动生成，仅供测试用途
class TopicAction extends Action {
	private $uid = '';
	/*======================================================================*\
		Function:	_initialize
		Purpose:	初始化一些参数，主要判断是否登录授权
		Input:		无	            空
		Output:		无      		空
	\*======================================================================*/
	public function _initialize(){
		
		$this->_uid = session('uid');
		if(!$this->_uid) $this->redirect("Public/login");//(503,"对不起，您登录超时了，请重新登录！",1);
		//B('Authenticate', $action);
	}
    public function index(){
		$uid = $this->_uid;
		//话题分类
		$TopicCategory = M('TopicCategory');
		$topic = $TopicCategory->where("status = 1")->select();
		//查询用户所在部门
		//$UserDepartment = M('UserDepartment');
		//话题列表
		$TopicPost = M('TopicPost');
		import('ORG.Util.Page');// 导入分页类
		$count = $TopicPost->where("status=1")->count();// 查询满足要求的总记录数
		$Page = new Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
		$show = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$topiclist = $TopicPost->alias('A')
							   ->join('oa_user_department B ON A.owner_uid = B.owner_uid')
							   ->join('oa_user_profile C ON C.owner_uid = A.owner_uid')
							   ->join('oa_topic_category E ON E.category_id = A.topic_categoryid')
							   ->join('oa_department D ON D.department_id = B.department_id')
							   ->field('A.*,B.*,C.name,D.department_name now_department_name,D.department_name pass_department_name,E.category_name')
							   ->where('A.status = 1')
							   ->order('update_time desc')
							  // ->limit($Page->firstRow.','.$Page->listRows)
							   ->select();
		//$sql = $TopPost->getLastSql();
		//dump($topiclist);
		//echo $sql;
		$this->assign("topiclist",$topiclist);
		//$this->assign('page',$show);// 赋值分页输出
		$this->assign("uid",$uid);
		$this->assign("list",$topic);
		//dump($topic);
		$this->display();
    }
	/*======================================================================*\
		Function:	postTopic
		Purpose:	发布新话题
		Input:		无	            空
		Output:		ajaxReturn
	\*======================================================================*/
	public function postTopic(){
		$TopicPost = M('TopicPost');
		$UserDepartment = M('UserDepartment');
		$param = $this->_param();
		$uid = $this->_uid;
		//查询登录用户所在的部门id
		$owner_groupid = $UserDepartment->where("owner_uid = $uid")->find();
		if($owner_groupid == null || $owner_groupid['department_id'] == -1){
			$this->ajaxReturn(-1,'你的身份尚在审核，无法发布话题！',-1);
		}
		if($owner_groupid['status'] == 0){
			$this->ajaxReturn(-2,'你已离职，无法发布话题！',-2);
		}
		$data = array(
			'topic_title' => $param['topic_title'],
			'topic_content' => $param['topic_content'],
			'owner_uid' => $uid,
			'owner_groupid' => $owner_groupid['department_id'],
			'topic_categoryid' => $param['category_id'],
			'create_time' => datetime(),
			'update_time' => datetime(),
			'status' => 1
		);
		$v = $TopicPost->add($data);
		if($v){
			$this->ajaxReturn(1,'话题发布成功！',1);
		}else{
			$this->ajaxReturn(0,'话题发布失败！',0);
		}
	}
	/*======================================================================*\
		Function:	topic
		Purpose:	查看话题详细内容
		Input:		无	            空
		Output:		display
	\*======================================================================*/
	public function topic(){
		$TopicPost = M('TopicPost');
		$param = $this->_param();
		$id = $param['id'];
		$view = $TopicPost->where("topic_id = $id")->setInc('view',1);
		if(!$view){
			$this->ajaxReturn(-1,'浏览记录增加1失败',-1);
		}
		$list = $TopicPost->where("topic_id = $id")->find();
		if($list){
			$this->ajaxReturn($list['topic_content'],'topic内容获取成功',1);
		}else{
			$this->ajaxReturn(0,'topic内容获取失败',0);
		}
	}
	/*======================================================================*\
		Function:	topicComments
		Purpose:	查看话题评论内容
		Input:		无	            空
		Output:		ajaxReturn
	\*======================================================================*/
	public function topicComments(){
	}
}